package com.aytbayev.asyl;

import java.util.LinkedList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.util.Log;

/**
 * This class is used for interaction with web services
 *
 */
public class DataManager {
	
	private static final String CHECK_CLIENT_SOAP_ACTION = "http://tempuri.org/checkClient";
	private static final String CHECK_CLIENT_METHOD_NAME = "checkClient";
	
	private static final String CHECK_CLIENT_MOBILE_SOAP_ACTION = "http://tempuri.org/checkClientMobileProxy";
	private static final String CHECK_CLIENT_MOBILE_METHOD_NAME = "checkClientMobileProxy";
	
	private static final String GET_INSTRUMENTS_SOAP_ACTION = "http://tempuri.org/GetInstrumentsKaseClient";
	private static final String GET_INSTRUMENTS_METHOD_NAME = "GetInstrumentsKaseClient";
	
	private static final String GET_INSTRUMENTS_MOBILE_SOAP_ACTION = "http://tempuri.org/GetInstrumentsKaseClientMobile";
	private static final String GET_INSTRUMENTS_MOBILE_METHOD_NAME = "GetInstrumentsKaseClientMobile";
		
	private static final String GET_REST_INSTRUMENTS_CLIENT_SOAP_ACTION = "http://tempuri.org/GetRestInstrClientProxy";
	private static final String GET_REST_INSTRUMENTS_CLIENT_METHOD_NAME = "GetRestInstrClientProxy";
	
	private static final String GET_REST_MONEY_SOAP_ACTION = "http://tempuri.org/GetRestMoneyClientProxy";
	private static final String GET_REST_MONEY_METHOD_NAME = "GetRestMoneyClientProxy";
	
	private static final String CHANGE_PASSWORD_MOBILE_SOAP_ACTION = "http://tempuri.org/changePasswordMobileProxy";
	private static final String CHANGE_PASSWORD_MOBILE_METHOD_NAME = "changePasswordMobileProxy";
		
	private static final String GET_GLASS_SOAP_ACTION = "http://tempuri.org/getGlass1";
	private static final String GET_GLASS_METHOD_NAME = "getGlass1";
	private static final String NAMESPACE = "http://tempuri.org/";
	//private static final String URL = "http://trading.ifdresmi.kz//Webservice1.asmx";
	private static final String URL = "https://asyl.newproject.kz/webservice1.asmx";
	
	
	public static String validateLogin(String login, String password){
		SoapObject request = new SoapObject(NAMESPACE, CHECK_CLIENT_METHOD_NAME);
		//request.addProperty("login", login);
        //request.addProperty("password", password.hashCode());
        //request.addProperty("p1", login.hashCode());
		
		request.addProperty("login", "tim");
		request.addProperty("password", "1846175709");
		request.addProperty("p1", "1344150661"); // 1846175709
		
        SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); // which version?
        soapEnvelope.dotNet = true;
        soapEnvelope.setOutputSoapObject(request);
        HttpTransportSE htse = new HttpTransportSE(URL);
        
        try{
        	htse.call(CHECK_CLIENT_SOAP_ACTION, soapEnvelope);      	  
        	SoapPrimitive result = (SoapPrimitive)soapEnvelope.getResponse(); // get response	   
        	return result.toString();  	      
        }catch(Exception e){
       	 	Log.e("BACKGROUND_PROC", e.getMessage());
       	 	return ""; // cound't connect to db
        } 
	}
	
	public static String validateMobileLogin(String login, String password) {
		SoapObject request = new SoapObject(NAMESPACE, CHECK_CLIENT_MOBILE_METHOD_NAME);
		request.addProperty("login", login);
		request.addProperty("password", password);
		
		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        soapEnvelope.dotNet = true;
        soapEnvelope.setOutputSoapObject(request);
        HttpTransportSE htse = new HttpTransportSE(URL);
        
        try{
        	htse.call(CHECK_CLIENT_MOBILE_SOAP_ACTION, soapEnvelope);      	  
        	SoapPrimitive result = (SoapPrimitive)soapEnvelope.getResponse(); // get response	   
        	return result.toString();  	      
        }catch(Exception e){
       	 	Log.e("BACKGROUND_PROC", e.getMessage());
       	 	return ""; // cound't connect to db
        }	
	}
	
	public static void fillInstruments(LinkedList<Instrument> instruments, String clientId){
		SoapObject request = new SoapObject(NAMESPACE, GET_INSTRUMENTS_METHOD_NAME);
        request.addProperty("clintId", clientId); 
        request.addProperty("typeInstr", "1");
        request.addProperty("demoMode", "0");
        
        SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        soapEnvelope.dotNet = true;
        soapEnvelope.setOutputSoapObject(request);
        HttpTransportSE htse = new HttpTransportSE(URL);
        
        try{
		  htse.call(GET_INSTRUMENTS_SOAP_ACTION, soapEnvelope);      	  
		  SoapObject result=(SoapObject)soapEnvelope.getResponse(); //get response  	    	       	      
		  int numInstruments = 0; 
		  if (result != null) {
			  numInstruments = result.getPropertyCount();
		  }
		  if(instruments.size() > 0){ 
			  instruments.clear();
		  }
		  for(int i=0; i < numInstruments; i++){
			  Instrument instrument = new Instrument(); 
			  Ksoap2ResultParser.parseObject(result.getProperty(i).toString(), instrument);      	   
			  instruments.add(instrument);       	    	 
		  }       	   
        } catch(Exception e) {
       	 	Log.e("BACKGROUND_PROC", e.getMessage());
        } 
	}
	
	public static void fillInstrumentsMobile(LinkedList<Instrument> instruments, String clientId){
		SoapObject request = new SoapObject(NAMESPACE, GET_INSTRUMENTS_MOBILE_METHOD_NAME);
        request.addProperty("clientId", clientId);
        request.addProperty("typeInstr", "1");
        request.addProperty("demoMode", "0");
        
        SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        soapEnvelope.dotNet = true;
        soapEnvelope.setOutputSoapObject(request);
        HttpTransportSE htse = new HttpTransportSE(URL);
        
        try{
		  htse.call(GET_INSTRUMENTS_MOBILE_SOAP_ACTION, soapEnvelope);      	  
		  SoapObject result=(SoapObject)soapEnvelope.getResponse(); //get response  	    	       	      
		  int numInstruments = 0; 
		  if (result != null) {
			  numInstruments = result.getPropertyCount();
		  }
		  if(instruments.size() > 0){ 
			  instruments.clear();
		  }
		  for(int i=0; i < numInstruments; i++){
			  Instrument instrument = new Instrument(); 
			  Ksoap2ResultParser.parseObject(result.getProperty(i).toString(), instrument);      	   
			  instruments.add(instrument);       	    	 
		  }       	   
        } catch(Exception e) {
       	 	Log.e("BACKGROUND_PROC", e.getMessage());
        } 
	}
	
	public static void fillStockOffers(LinkedList<StockOffer> stock_offers, String idKase, String typeGlass){
		SoapObject request = new SoapObject(NAMESPACE, GET_GLASS_METHOD_NAME);
        
        //request.addProperty("instrumentId", idKase);
        //request.addProperty("real", "1");
		request.addProperty("idKase", idKase);
		request.addProperty("demoMode", 0);
		request.addProperty("typeGlass", typeGlass);
		request.addProperty("ticker", "");
		
        SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); // which version?
        soapEnvelope.dotNet = true;
        soapEnvelope.setOutputSoapObject(request);
        HttpTransportSE htse = new HttpTransportSE(URL);
        
        try {
        	htse.call(GET_GLASS_SOAP_ACTION, soapEnvelope);
  
			SoapObject result = (SoapObject)soapEnvelope.getResponse(); // get response  	    	       	      
			int numOffers = 0;
			if (result != null){
				numOffers = result.getPropertyCount(); 	
			}			
			if(stock_offers.size() > 0){ 
				stock_offers.clear();
			}
			for(int i=0; i < numOffers; i++){
				StockOffer offer = new StockOffer(); 
				Ksoap2ResultParser.parseObject(result.getProperty(i).toString(), offer);
				stock_offers.add(offer);       	    	 
			}       	   
        } catch (Exception e){
        	Log.e("BACKGROUND_PROC", e.getMessage());
        } 
	}
	
	public static void fillMyInstruments(LinkedList<MyInstrument> myInstruments, String clientId) {
		SoapObject request = new SoapObject(NAMESPACE, GET_REST_INSTRUMENTS_CLIENT_METHOD_NAME);
		request.addProperty("client_id", clientId); 
		request.addProperty("demoMode", 0);
				
		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); // which version?
        soapEnvelope.dotNet = true;
        soapEnvelope.setOutputSoapObject(request);
        HttpTransportSE htse = new HttpTransportSE(URL);
                
        try {
        	htse.call(GET_REST_INSTRUMENTS_CLIENT_SOAP_ACTION, soapEnvelope);
  
			SoapObject result = (SoapObject)soapEnvelope.getResponse(); // get response  	    	       	      
			int numInstr = 0;
			if (result != null){
				numInstr = result.getPropertyCount(); 	
			}
			if(myInstruments.size() > 0) {
				myInstruments.clear();
			}
			for (int i=0; i < numInstr; i++) {
				MyInstrument curr_instrument = new MyInstrument(); 
				Ksoap2ResultParser.parseObject(result.getProperty(i).toString(), curr_instrument);
				myInstruments.add(curr_instrument);       	    	 
			}       	   
        } catch (Exception e){
        	Log.e("Parsing MyInstruments", e.getMessage());
        } 
	}

	public static void fillMyMoney(LinkedList<CurrencyItem> money, String clientId){
		SoapObject request = new SoapObject(NAMESPACE, GET_REST_MONEY_METHOD_NAME);
        request.addProperty("client_id", clientId); 
        request.addProperty("demoMode", "0");
        
        SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        soapEnvelope.dotNet = true;
        soapEnvelope.setOutputSoapObject(request);
        HttpTransportSE htse = new HttpTransportSE(URL);
        
        try{
		  htse.call(GET_REST_MONEY_SOAP_ACTION, soapEnvelope);      	  
		  SoapObject result = (SoapObject)soapEnvelope.getResponse(); //get response  	    	       	      
		  int count = 0;
		  if (result != null) {
			  count = result.getPropertyCount();
		  }
		  if(money.size() > 0){ 
			  money.clear();
		  }
		  for(int i=0; i < count; i++){
			  CurrencyItem item = new CurrencyItem(); 
			  Ksoap2ResultParser.parseObject(result.getProperty(i).toString(), item);      	   
			  money.add(item);       	    	 
		  }       	   
        } catch(Exception e) {
       	 	Log.e("BACKGROUND_PROC", e.getMessage());
        } 
	}
	
	public static String changePasswordMobile(int clientId, String oldPassword, String newPassword){
		SoapObject request = new SoapObject(NAMESPACE, CHANGE_PASSWORD_MOBILE_METHOD_NAME);
		request.addProperty("clientId", clientId);
		request.addProperty("oldPassowrd", oldPassword);
		request.addProperty("newPassword", newPassword);
		
		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        soapEnvelope.dotNet = true;
        soapEnvelope.setOutputSoapObject(request);
        HttpTransportSE htse = new HttpTransportSE(URL);
        
        try{
        	htse.call(CHANGE_PASSWORD_MOBILE_SOAP_ACTION, soapEnvelope);      	  
        	SoapPrimitive result = (SoapPrimitive)soapEnvelope.getResponse();	   
        	return result.toString();  	      
        } catch(Exception e) {
       	 	Log.e("BACKGROUND_PROC", e.getMessage());
       	 	return ""; // cound't connect to db
        }
		
	}
}
